package sort

//快速排序，传入数组和排序方式顺序或倒序
func QuickSort(array []int, desc bool) []int {
	//数组长度
	arrayLen := len(array)
	//数组长度小于2不用排序直接返回
	if arrayLen < 2 {
		return array
	}
	//第一个数据
	splitData := array[0]
	//比我小的数据
	low := make([]int, 0, 0)
	//比我大的数据
	high := make([]int, 0, 0)
	//与我一样大的数据
	mid := make([]int, 0, 0)
	//加入一个
	mid = append(mid, splitData)
	for i := 1; i < arrayLen; i++ {
		if array[i] < splitData {
			low = append(low, array[i])
		} else if array[i] > splitData {
			high = append(high, array[i])
		} else {
			mid = append(mid, array[i])
		}
	}
	low, high = QuickSort(low, desc), QuickSort(high, desc)
	if desc {
		myArray := append(append(high, mid...), low...)
		return myArray
	} else {
		myArray := append(append(low, mid...), high...)
		return myArray
	}
}
